package com.canxin.controller;
import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import org.jeecgframework.core.common.controller.BaseController;
import org.jeecgframework.core.common.hibernate.qbc.CriteriaQuery;
import org.jeecgframework.core.common.model.json.AjaxJson;
import org.jeecgframework.core.common.model.json.DataGrid;
import org.jeecgframework.core.constant.Globals;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.tag.core.easyui.TagUtil;
import org.jeecgframework.web.system.pojo.base.TSDepart;
import org.jeecgframework.web.system.service.SystemService;
import org.jeecgframework.core.util.MyBeanUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.canxin.entity.StudentsEntity;
import com.canxin.service.ExamServiceI;
import com.canxin.service.StudentServiceI;
import com.google.gson.JsonArray;

/**   
 * @Title: Controller
 * @Description: 学生信息
 * @author zhangdaihao
 * @date 2017-03-05 15:40:09
 * @version V1.0   
 *
 */
@Scope("prototype")
@Controller
@RequestMapping("/studentController")
public class StudentController extends BaseController {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(StudentController.class);

	@Autowired
	private StudentServiceI studentService;
	@Autowired
	private ExamServiceI examServiceI;
	@Autowired
	private SystemService systemService;
	private String message;
	
	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}


	/**
	 * 学生信息列表 页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "studentExams")
	public ModelAndView student(HttpServletRequest request) {
		
		return new ModelAndView("com/canxin/student/studentList");
	}

/*	@ResponseBody
	@RequestMapping(params = "searchStudent",produces="application/json;charset=UTF-8")
	public void searchStudent(HttpServletRequest request,HttpServletResponse resp,@RequestParam("studentName") String studentName,
		@RequestParam("callback") String callback) throws IOException {
		logger.info(studentName);
		List<StudentsEntity> studentEntityList = studentService.getStudentsByStudentName(studentName);
		JSONArray jsonArray = new JSONArray();
		for (StudentsEntity studentEntity : studentEntityList) {
			jsonArray.add(studentEntity);
		}
		System.out.println(jsonArray.toJSONString());
		resp.getWriter().write(callback+"("+jsonArray.toJSONString()+")");
		resp.getWriter().flush();
		resp.getWriter().close();
	}*/

	@ResponseBody
	@RequestMapping(params = "searchStudent",produces="application/json;charset=UTF-8")
	public AjaxJson searchStudent(HttpServletRequest request,HttpServletResponse resp,@RequestParam("studentName") String studentName
							 ) throws IOException {
		logger.info(studentName);
		List<StudentsEntity> studentEntityList = studentService.getStudentsByStudentName(studentName);
		JSONArray jsonArray = new JSONArray();
		for (StudentsEntity studentEntity : studentEntityList) {
			jsonArray.add(studentEntity);
		}
		System.out.println(jsonArray.toJSONString());
		AjaxJson result = new AjaxJson();
		result.setSuccess(true);
		result.setObj(jsonArray);
		return result;
	}

	/**
	 * easyui AJAX请求数据
	 * 
	 * @param request
	 * @param response
	 * @param dataGrid
	 * @param user
	 */

	@RequestMapping(params = "datagrid")
	public void datagrid(StudentsEntity student,HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) {
		CriteriaQuery cq = new CriteriaQuery(StudentsEntity.class, dataGrid);
		//查询条件组装器
		org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, student, request.getParameterMap());
		this.studentService.getDataGridReturn(cq, true);
		TagUtil.datagrid(response, dataGrid);
	}

	/**
	 * 删除学生信息
	 * 
	 * @return
	 */
	@RequestMapping(params = "del")
	@ResponseBody
	public AjaxJson del(StudentsEntity student, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		student = systemService.getEntity(StudentsEntity.class, student.getId());
		message = "学生信息删除成功";
		studentService.delete(student);
		systemService.addLog(message, Globals.Log_Type_DEL, Globals.Log_Leavel_INFO);
		
		j.setMsg(message);
		return j;
	}


	/**
	 * 添加学生信息
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping(params = "save")
	@ResponseBody
	public AjaxJson save(StudentsEntity student, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		if (StringUtil.isNotEmpty(student.getId())) {
			message = "学生信息更新成功";
			StudentsEntity t = studentService.get(StudentsEntity.class, student.getId());
			try {
				MyBeanUtils.copyBeanNotNull2Bean(student, t);
				studentService.saveOrUpdate(t);
				systemService.addLog(message, Globals.Log_Type_UPDATE, Globals.Log_Leavel_INFO);
			} catch (Exception e) {
				e.printStackTrace();
				message = "学生信息更新失败";
			}
		} else {
			message = "学生信息添加成功";
			studentService.save(student);
			systemService.addLog(message, Globals.Log_Type_INSERT, Globals.Log_Leavel_INFO);
		}
		j.setMsg(message);
		return j;
	}

	/**
	 * 学生信息列表页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "addorupdate")
	public ModelAndView addorupdate(StudentsEntity student, HttpServletRequest req) {
		if (StringUtil.isNotEmpty(student.getId())) {
			student = studentService.getEntity(StudentsEntity.class, student.getId());
			req.setAttribute("studentPage", student);
		}
		return new ModelAndView("com/canxin/student/student");
	}
}
